package com.lepu.app.fun.tool.tefypc;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.DensityUtil;
import com.core.lib.utils.main.UtilityBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.tefypc.bean.BeanToolTaiEr;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class TaiErResultActivity extends BaseFragmentActivity implements 
    OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {
	
    public TextView mNumberText;
    public LinearLayout mValueLayout1;
    public LinearLayout mValueLayout2;
    public LinearLayout mValueLayout3;
    public TextView mValueText1;
    public TextView mValueText2;
    public TextView mValueText3;
    
    public TextView acText1;  // 最小值
    public TextView acText2;  // 平均值
    public TextView acText3;  // 最大值
    public TextView bpdText1;  // 最小值
    public TextView bpdText2;  // 平均值
    public TextView bpdText3;  // 最大值
    public TextView flText1;  // 最小值
    public TextView flText2;  // 平均值
    public TextView flText3;  // 最大值
    
    public final int RULER = 170; //尺子宽度
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_taier_result_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_taidong, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	@Override
	public void onTopbarRightButtonSelected() {
		startActivity(new Intent(this, TaiErShowActivity.class),true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("评测结果");
		topbar.setRightText("参考数据");
		topbar.setTopbarBackground(R.color.color_taidong);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mNumberText = (TextView) findViewById(R.id.number);
		mValueLayout1 = (LinearLayout) findViewById(R.id.valueLayout1);
		mValueLayout2 = (LinearLayout) findViewById(R.id.valueLayout2);
		mValueLayout3 = (LinearLayout) findViewById(R.id.valueLayout3);
		mValueText1 = (TextView) findViewById(R.id.valueText1);
		mValueText2 = (TextView) findViewById(R.id.valueText2);
		mValueText3 = (TextView) findViewById(R.id.valueText3);
		
		acText1 = (TextView) findViewById(R.id.ac_dial1);
		acText2 = (TextView) findViewById(R.id.ac_dial2);
		acText3 = (TextView) findViewById(R.id.ac_dial3);
		bpdText1 = (TextView) findViewById(R.id.bpd_dial1);
		bpdText2 = (TextView) findViewById(R.id.bpd_dial2);
		bpdText3 = (TextView) findViewById(R.id.bpd_dial3);
		flText1 = (TextView) findViewById(R.id.fl_dial1);
		flText2 = (TextView) findViewById(R.id.fl_dial2);
		flText3 = (TextView) findViewById(R.id.fl_dial3);
		
		//value
		String week = getIntent().getStringExtra("week");
		String ac = getIntent().getStringExtra("ac");
		String bpd = getIntent().getStringExtra("bpd");
		String fl = getIntent().getStringExtra("fl");
		mNumberText.setText(week);
		mValueText1.setText(ac);
		mValueText2.setText(bpd);
		mValueText3.setText(fl);
		
		//刻度
		BeanToolTaiEr bean = new BeanToolTaiEr();
		ArrayList<BeanToolTaiEr> beanList= BeanToolTaiEr.getData(week);
		if(beanList.size() > 0){
			bean = beanList.get(0);
		}
		
		acText1.setText(String.valueOf(bean.ACMin));
		acText2.setText(String.valueOf(bean.ACAvarage));
		acText3.setText(String.valueOf(bean.ACMax));
		bpdText1.setText(String.valueOf(bean.BPDMin));
		bpdText2.setText(String.valueOf(bean.BPDAvarage));
		bpdText3.setText(String.valueOf(bean.BPDMax));
		flText1.setText(String.valueOf(bean.FLMin));
		flText2.setText(String.valueOf(bean.FLAvarage));
		flText3.setText(String.valueOf(bean.FLMax));

		//腰围
		double acValue = Double.valueOf(ac);

		double acMinValue = Double.valueOf(bean.ACMin);
		double acMaxValue = Double.valueOf(bean.ACMax);
		double acAvarageValue = Double.valueOf(bean.ACAvarage);
		float acMar = 0;

		if(acValue<acMinValue){
			mValueLayout1.setBackgroundResource(R.drawable.tool_taier_value_bg1);
			acMar = 0;
		}else if(acValue >= acMinValue && acValue <=acMaxValue){
			mValueLayout1.setBackgroundResource(R.drawable.tool_taier_value_bg2);
			if(acValue == acAvarageValue){
				acMar = getMargin(1, 3, 2);
			}else{
				acMar = getMargin(acMinValue, acMaxValue, acValue);
			}
			
		}else if(acValue > acMaxValue){
			mValueLayout1.setBackgroundResource(R.drawable.tool_taier_value_bg3);
			acMar = RULER+25;
		}
		
		LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
		lp1.setMargins(DensityUtil.dip2px(this, acMar), 0, 0, 0);  
		mValueLayout1.setLayoutParams(lp1);  
		
		//双顶
		double bpdValue = Double.valueOf(bpd);

		double bpdMinValue = Double.valueOf(bean.BPDMin);
		double bpdMaxValue = Double.valueOf(bean.BPDMax);
		double bpdAvarageValue = Double.valueOf(bean.BPDAvarage);
		float bpdMar = 0;

		if(bpdValue<bpdMinValue){
			mValueLayout2.setBackgroundResource(R.drawable.tool_taier_value_bg1);
			bpdMar = 0;
		}else if(bpdValue >= bpdMinValue && bpdValue <=bpdMaxValue){
			mValueLayout2.setBackgroundResource(R.drawable.tool_taier_value_bg2);
			if(bpdValue == bpdAvarageValue){
				bpdMar = getMargin(1, 3, 2);
			}else{
				bpdMar = getMargin(bpdMinValue, bpdMaxValue, bpdValue);
			}
			
		}else if(bpdValue > bpdMaxValue){
			mValueLayout2.setBackgroundResource(R.drawable.tool_taier_value_bg3);
			bpdMar=RULER+25;
		}
		
		LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
		lp2.setMargins(DensityUtil.dip2px(this, bpdMar), 0, 0, 0);  
		mValueLayout2.setLayoutParams(lp2);  
		
		//股骨
		double flValue = Double.valueOf(fl);

		double flMinValue = Double.valueOf(bean.FLMin);
		double flMaxValue = Double.valueOf(bean.FLMax);
		double flAvarageValue = Double.valueOf(bean.FLAvarage);
		float flMar = 0;

		if(flValue<flMinValue){
			mValueLayout3.setBackgroundResource(R.drawable.tool_taier_value_bg1);
			flMar = 0;
		}else if(flValue >= flMinValue && flValue <=flMaxValue){
			mValueLayout3.setBackgroundResource(R.drawable.tool_taier_value_bg2);
			if(flValue == flAvarageValue){
				flMar = getMargin(1, 3, 2);
			}else{
				flMar = getMargin(flMinValue, flMaxValue, flValue);
			}
		
		}else if(flValue > flMaxValue){
			mValueLayout3.setBackgroundResource(R.drawable.tool_taier_value_bg3);
			flMar = RULER+25;
		}
		
		LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
		lp3.setMargins(DensityUtil.dip2px(this, flMar), 0, 0, 0);  
		mValueLayout3.setLayoutParams(lp3);  
		
	}
	
	public int getMargin(double min, double max, double value){
		float ruler = ((float)(value - min)) / ((float)(max - min));
		int rulerValue = (int)(ruler * RULER);
		return rulerValue + 8;
	}

}
